package com.neusoft.yuejian26.yuejian_26.Activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.neusoft.yuejian26.yuejian_26.Bean.ActSimpbean;

import com.neusoft.yuejian26.yuejian_26.Bean.Result;
import com.neusoft.yuejian26.yuejian_26.Other.CacheActInfo;
import com.neusoft.yuejian26.yuejian_26.Other.CacheInfo;
import com.neusoft.yuejian26.yuejian_26.R;
import com.neusoft.yuejian26.yuejian_26.model.ActivityModel;
import com.neusoft.yuejian26.yuejian_26.model.AttentionModel;
import com.neusoft.yuejian26.yuejian_26.network.RetrofitCallback;
import com.squareup.picasso.Picasso;

import retrofit2.Call;

public class ActSimpActivity extends AppCompatActivity {

    TextView simp_title,simp_act_name,simp_act_address,simp_act_time,simp_act_cost,simp_act_tel,simp_info;
    RelativeLayout btn_apply;
    ImageView act_simp_pic,home_act_attention,svg_back;
    String actid=null;
    String mid=null;
    String uid=null;
    String xid;
    String flag=String.valueOf(3);
    int attentionState;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_act_simp);
        intview();
        intEvent();
        getlastActivityData();
        getActSimpData();
    }

    private void getActSimpData() {
        ActivityModel activityModel=new ActivityModel();
        Call call=activityModel.actSimp(actid);
        activityModel.bindCallback(call, new RetrofitCallback() {
            @Override
            public void onResponse(Object o) {
                ActSimpbean msimpdata= (ActSimpbean) o;
                Picasso.with(ActSimpActivity.this)
                        .load("http://115.159.93.244/new18yuejian/Public/upload/"+msimpdata.getActpic())
                        .into(act_simp_pic);
                simp_act_name.setText(msimpdata.getAname());
                simp_act_address.setText(msimpdata.getActaddress());
                simp_act_time.setText(msimpdata.getActdata());
                simp_act_cost.setText(msimpdata.getActp()+"/人");
                simp_act_tel.setText(msimpdata.getActpone());
                simp_info.setText(msimpdata.getActintro());
                getAttentionState();
            }
        });
    }

    private void getAttentionState(){
        uid = new CacheInfo(ActSimpActivity.this).getUserIdCache();
        Bundle bundle=this.getIntent().getExtras();
        xid = bundle.getString("actiid");
        AttentionModel attentionModel = new AttentionModel();
        Call call = attentionModel.checkState(uid,xid,flag);
        attentionModel.bindCallback(call, new RetrofitCallback() {
            @Override
            public void onResponse(Object o) {
                Result checkState = (Result) o;
                int state = checkState.getCode();
                attentionState = state;
                if(1==state){
                    home_act_attention.setImageResource(R.drawable.ic_home_attention_unchecked);
                }
                else {
                    home_act_attention.setImageResource(R.drawable.ic_home_attention);
                }
            }
        });
    }

    private class doAttention implements View.OnClickListener {

        @Override
        public void onClick(View view) {
            AttentionModel attentionModel = new AttentionModel();
            Call call = attentionModel.doAttention(uid,xid,flag);
            attentionModel.bindCallback(call, new RetrofitCallback() {
                @Override
                public void onResponse(Object o) {
                    Result doAttention = (Result) o;
                    int states = doAttention.getCode();
                    if (1==states){
                        if (attentionState==1){
                            Toast.makeText(ActSimpActivity.this,"关注活动成功",Toast.LENGTH_LONG).show();
                            attentionState = 0;
                            home_act_attention.setImageResource(R.drawable.ic_home_attention);
                        }
                        else {
                            Toast.makeText(ActSimpActivity.this,"取消关注成功",Toast.LENGTH_LONG).show();
                            attentionState = 1;
                            home_act_attention.setImageResource(R.drawable.ic_home_attention_unchecked);
                        }
                    }
                }
            });
        }
    }

    private void getlastActivityData(){
        Bundle bundle=this.getIntent().getExtras();
        actid = bundle.getString("actiid");
        simp_title.setText(bundle.getString("actname"));
    }

    private  void intview(){
        svg_back =(ImageView) findViewById(R.id.svg_back);
        act_simp_pic=(ImageView) findViewById(R.id.act_simp_pic);
        simp_title=(TextView) findViewById(R.id.simp_title);
        home_act_attention= (ImageView) findViewById(R.id.home_act_attention);
        simp_act_name=(TextView) findViewById(R.id.simp_act_name);
        simp_act_address=(TextView) findViewById(R.id.simp_act_address);
        simp_act_time=(TextView) findViewById(R.id.simp_act_time);
        simp_act_cost=(TextView) findViewById(R.id.simp_act_cost);
        simp_act_tel=(TextView) findViewById(R.id.simp_act_tel);
        simp_info=(TextView) findViewById(R.id.simp_info);
        btn_apply=(RelativeLayout) findViewById(R.id.btn_apply);
    }

    private  void intEvent(){
        home_act_attention.setOnClickListener(new doAttention());
        svg_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(ActSimpActivity.this,MainActivity.class);
                Bundle bundle = new Bundle();
                bundle.putInt("page",0);
                intent.putExtras(bundle);
                startActivity(intent);
                finish();
            }
        });
        btn_apply.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                CacheActInfo cacheActInfo = new CacheActInfo(ActSimpActivity.this);
                cacheActInfo.setActIdCache(actid + "");
                Intent intent = new Intent(ActSimpActivity.this, ApplyActivity.class);
                startActivity(intent);
            }
        });
    }


}
